package frame.options.sdn.create;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import model.components.networks.sdn.SdnNetworks;

import com.nexes.wizard.WizardPanelDescriptor;


public class CreateSdnSelectDescriptor extends WizardPanelDescriptor implements DocumentListener, ActionListener
{
	public static final String IDENTIFIER = CreateSdnSelectDescriptor.class.getSimpleName();
    
	private CreateSdnSelectPanel createSdnSelectPanel;
	public CreateSdnSelectPanel getCreateSdnSelectPanel(){ return createSdnSelectPanel; }
	public void setCreateSdnSelectPanel( CreateSdnSelectPanel createSdnSelectPanel ){ this.createSdnSelectPanel = createSdnSelectPanel; }
	
	private CreateSdnWizard createSdnWizard;
	public CreateSdnWizard getCreateSdnWizard(){ return createSdnWizard; }
	public void setCreateSdnWizard( CreateSdnWizard createSdnWizard ){ this.createSdnWizard = createSdnWizard; }
	
	public CreateSdnSelectDescriptor()
    {
    	this.setCreateSdnSelectPanel( new CreateSdnSelectPanel() );
    	this.getCreateSdnSelectPanel().addListeners( this, this );
        this.setPanelDescriptorIdentifier( IDENTIFIER );
        this.setPanelComponent( this.getCreateSdnSelectPanel() );
    }
    
	@Override
	public Object getBackPanelDescriptor()
    {
        return null;
    }
	@Override
	public Object getNextPanelDescriptor()
    {
        return CreateSdnFinishDescriptor.IDENTIFIER;
    }
	@Override
	public void aboutToDisplayPanel()
    {
    	this.setCreateSdnWizard( (CreateSdnWizard) this.getWizard() );
    	this.getCreateSdnWizard().setNextFinishButtonEnabled( false );
    	this.enableFinishButton();
    }
	public void aboutToHidePanel()
	{
		this.createSdnNetworks();
	}
	private void createSdnNetworks()
	{
		CreateSdnSelectPanel createSdnSelectPanel = this.getCreateSdnSelectPanel();
		
		int networksNumber = Integer.parseInt( createSdnSelectPanel.getNetworksNumberTextField().getText() );
		
		int minRoutersNumber = Integer.parseInt( createSdnSelectPanel.getMinRoutersNumberTextField().getText() );
    	int maxRoutersNumber = Integer.parseInt( createSdnSelectPanel.getMaxRoutersNumberTextField().getText() );

		int minSwitchesNumber = Integer.parseInt( createSdnSelectPanel.getMinSwitchesNumberTextField().getText() );
    	int maxSwitchesNumber = Integer.parseInt( createSdnSelectPanel.getMaxSwitchesNumberTextField().getText() );
		
    	int minServersNumber = Integer.parseInt( createSdnSelectPanel.getMinServersNumberTextField().getText() );
    	int maxServersNumber = Integer.parseInt( createSdnSelectPanel.getMaxServersNumberTextField().getText() );
    	
    	int linuxPercentage = Integer.parseInt( createSdnSelectPanel.getLinuxPercentageTextField().getText() );
		int windowsPercentage = Integer.parseInt( createSdnSelectPanel.getWindowsPercentageTextField().getText() );
		int solarisPercentage = Integer.parseInt( createSdnSelectPanel.getSolarisPercentageTextField().getText() );
		int androidPercentage = Integer.parseInt( createSdnSelectPanel.getAndroidPercentageTextField().getText() );
		
		int minCPU = Integer.parseInt( createSdnSelectPanel.getMinCPUtextField().getText() );
		int maxCPU = Integer.parseInt( createSdnSelectPanel.getMaxCPUtextField().getText() );
		int minCPUprice = Integer.parseInt( createSdnSelectPanel.getMinCPUpriceTextField().getText() );
		int maxCPUprice = Integer.parseInt( createSdnSelectPanel.getMaxCPUpriceTextField().getText() );
		
		int minRAM = Integer.parseInt( createSdnSelectPanel.getMinRAMtextField().getText() );
		int maxRAM = Integer.parseInt( createSdnSelectPanel.getMaxRAMtextField().getText() );
		int minRAMprice = Integer.parseInt( createSdnSelectPanel.getMinRAMpriceTextField().getText() );
		int maxRAMprice = Integer.parseInt( createSdnSelectPanel.getMaxRAMpriceTextField().getText() );
		
		int minStorage = Integer.parseInt( createSdnSelectPanel.getMinStorageTextField().getText() );
		int maxStorage = Integer.parseInt( createSdnSelectPanel.getMaxStorageTextField().getText() );
		int minStoragePrice = Integer.parseInt( createSdnSelectPanel.getMinStoragePriceTextField().getText() );
		int maxStoragePrice = Integer.parseInt( createSdnSelectPanel.getMaxStoragePriceTextField().getText() );
		
		int maxVirtualNodesNumber = Integer.parseInt( createSdnSelectPanel.getMaxVirtualNodesNumberTextField().getText() );
		
		int minBW = Integer.parseInt( createSdnSelectPanel.getMinBWtextField().getText() );
		int maxBW = Integer.parseInt( createSdnSelectPanel.getMaxBWtextField().getText() );
		int minBWprice = Integer.parseInt( createSdnSelectPanel.getMinBWpriceTextField().getText() );
		int maxBWprice = Integer.parseInt( createSdnSelectPanel.getMaxBWpriceTextField().getText() );
		
		int minDistance = Integer.parseInt( createSdnSelectPanel.getMinDistanceTextField().getText() );
		int maxDistance = Integer.parseInt( createSdnSelectPanel.getMaxDistanceTextField().getText() );
		
		SdnNetworks sdnNetworks = new SdnNetworks( SdnNetworks.NAME_PREFIX, networksNumber, minRoutersNumber, maxRoutersNumber, minSwitchesNumber, maxSwitchesNumber, minServersNumber, maxServersNumber, linuxPercentage, windowsPercentage, solarisPercentage, androidPercentage, minCPU, maxCPU, minCPUprice, maxCPUprice, minRAM, maxRAM, minRAMprice, maxRAMprice, minStorage, maxStorage, minStoragePrice, maxStoragePrice, maxVirtualNodesNumber, minBW, maxBW, minBWprice, maxBWprice, minDistance, maxDistance );
		this.getCreateSdnWizard().setSdnNetworks( sdnNetworks.getSdnNetworks() );
	}
	
	@Override
	public void actionPerformed( ActionEvent actionEvent )
	{
		this.enableFinishButton();
	}
	@Override
    public void changedUpdate( DocumentEvent documentEvent )
    {
    	this.enableFinishButton();
    }
	@Override
	public void insertUpdate( DocumentEvent documentEvent )
	{
		this.enableFinishButton();
	}
	@Override
	public void removeUpdate( DocumentEvent documentEvent )
	{
		this.enableFinishButton();
	}
	
	private void enableFinishButton()
	{
		if( this.getCreateSdnSelectPanel().check() )
			this.getCreateSdnWizard().setNextFinishButtonEnabled( true );
		else
        	this.getCreateSdnWizard().setNextFinishButtonEnabled( false );
    }
	
}
